#include "simulate.h"
#include "ui_simulate.h"
#include "convert.h"
#include <QDebug>

Simulate::Simulate(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Simulate)
{
    ui->setupUi(this);

    connect(ui->checkBox, &QCheckBox::toggled, this, &Simulate::on_checkBox);
    connect(&socket, &QUdpSocket::readyRead, this, &on_socketReceiv);
}

Simulate::~Simulate()
{
    delete ui;
}

void Simulate::on_checkBox()
{
    if(ui->checkBox->isChecked()) {
        socket.bind(ui->spinBox->value());

        if(socket.isValid()) {
            ui->spinBox->setEnabled(false);
            statusBar()->showMessage(tr("本地端口邦定成功！"), 5000);
            ui->tabWidget->setEnabled(true);
        }
    } else {
        socket.close();
        ui->spinBox->setEnabled(true);
        statusBar()->showMessage(tr("解出本地邦定！"), 5000);
        ui->tabWidget->setEnabled(false);
    }
}

void Simulate::on_socketReceiv()
{
    QByteArray ret;
    ret.resize(socket.pendingDatagramSize());
    QHostAddress ip;
    quint16 port;

    socket.readDatagram(ret.data(), socket.pendingDatagramSize(), &ip, &port);
    qDebug() << ret;

    if(ret.size() != 2) return;

    if(ret[0] == (char)0x01) {
        switch(ret.at(1)) {
        case(char)0xA4: {
            QByteArray dat = Convert::toByteArray(ui->textEdit_A4->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }

        case(char)0xB4: {
            QByteArray dat = Convert::toByteArray(ui->textEdit_B4->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }
        case(char)0xBB: {
            QByteArray dat = Convert::toByteArray(ui->textEdit_BB->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }
        case(char)0xC0:{
            QByteArray dat = Convert::toByteArray(ui->textEdit_C0->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }
        case(char)0xC2:{
            QByteArray dat = Convert::toByteArray(ui->textEdit_C2->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }
        case(char)0xAF:{
            QByteArray dat = Convert::toByteArray(ui->textEdit_AF->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }
        case(char)0xA3:{
            QByteArray dat = Convert::toByteArray(ui->textEdit_A3->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }
        case(char)0x8B:{
            QByteArray dat = Convert::toByteArray(ui->textEdit_8B->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }
        case(char)0x8d:{
            QByteArray dat = Convert::toByteArray(ui->textEdit_8D->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }
        case(char)0xBD:{
            QByteArray dat = Convert::toByteArray(ui->textEdit_BD->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }
        case(char)0x87:{
            QByteArray dat = Convert::toByteArray(ui->textEdit_87->toPlainText());
            socket.writeDatagram(dat, ip, port);
            break;
        }

        default:
            break;
        }
    }
}
